﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLBH.Models;
using QLBH.Utilities;
using QLBH.Controllers;

namespace QLBH.Modules
{

    public partial class frmAdd_Edit_Branchs : Form
    {
        
        private eType type = eType.ADD;
        private BranchsModel objmodel;
        public BranchsModel objModel
        {
            get { return objmodel; }
            set { objmodel = value; }
        }


        public frmAdd_Edit_Branchs(eType type)
        {
            objmodel = new BranchsModel();
            InitializeComponent();
            this.type = type;
        }

        private void listwarehouse()
        {
            List<WarehouseModel> data = WSAccess.GetData<List<WarehouseModel>>(MainForm.Instance.Shop_id.ToString() + "/warehouse");

            cbbKhohang.DataSource = data;
            cbbKhohang.ValueMember = "id";
            cbbKhohang.DisplayMember = "name";
        }
        private void frmAdd_Edit_Branchs_Load(object sender, EventArgs e)
        {
            listwarehouse();
            if (type == eType.EDIT)
            {
                txbTen.Text = objModel.Name;
                txbSodt.Text = objModel.Phone;
                txbDiachi.Text = objModel.Address;
                cbbKhohang.SelectedValue = int.Parse(objModel.Warehourse_id.ToString());
            }
        }
        public bool Checkvalidate()
        {
            try
            {
                DataInputHelper.CheckTextData(txbTen.Text, "Tên chi nhánh", 255);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Quản lý chi nhánh", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txbTen.SelectAll();
                txbTen.Focus();
                return false;
            }
            try
            {
                DataInputHelper.CheckTextData(txbDiachi.Text, "Địa chỉ", 255);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Quản lý chi nhánh", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txbDiachi.SelectAll();
                txbDiachi.Focus();
                return false;
            }
            try
            {
            DataInputHelper.CheckPhoneNumber(txbSodt.Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Quản lý chi nhánh", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txbSodt.SelectAll();
                txbSodt.Focus();
                return false;
            }
            return true;
        }
        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (Checkvalidate())
            {
                int shop_id = int.Parse(MainForm.Instance.Shop_id.ToString());
                objModel.Name = txbTen.Text;
                objModel.Address = txbDiachi.Text;
                objModel.Phone = txbSodt.Text;
                objModel.Warehourse_id = int.Parse(cbbKhohang.SelectedValue.ToString());
                objModel.Shop_id = shop_id;

                try
                {

                    if (type == eType.ADD)
                    {
                        try
                        {
                            InsertResponse response = WSAccess.Insert<BranchsModel, InsertResponse>(objModel, shop_id + "/branch/register");
                            if (response.Status != null)
                            {
                                MessageBox.Show("Đăng ký thành công");
                            }
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }

                    }
                    else
                    {
                        try
                        {
                            WSAccess.Update<BranchsModel>(objModel, objModel.Id + "/branch/edit");
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }

                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                this.DialogResult = DialogResult.None;
            }
        }
        
        private void btnHuy_Click(object sender, EventArgs e)
        {
            
        }

        private void btnThemKho_Click(object sender, EventArgs e)
        {
            frmAdd_Edit_Warehouse frmWarehouse = new frmAdd_Edit_Warehouse(eType.ADD);
            frmWarehouse.ShowDialog();
        }

    }


    public class InsertResponse : ResponseBase
    {
        private int branch_id;
        public int Branch_id
        {
            get { return branch_id; }
            set { branch_id = value; }
        }
    }
}
